package com.qingke.factory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.qingke.pojos.MyBeanDefinition;

public class MyBeanFactory {
	private static List<MyBeanDefinition> list;
	private static Map<String,Object> singletonBean;
	private static Map<String,MyBeanDefinition> prototypeBean;
	
	public MyBeanFactory(){
		list = new ArrayList<MyBeanDefinition>();
		singletonBean = new HashMap<String,Object>();
		prototypeBean = new HashMap<String,MyBeanDefinition>();
		parse();
		resolveBeanDefinition();
	}
	
	public Object getBean(String id){
		if(singletonBean.containsKey(id)){
			return singletonBean.get(id);
		}
		
		MyBeanDefinition myBeanDefinition = prototypeBean.get(id);
		try {
			Object obj = Class.forName(myBeanDefinition.getClassName()).newInstance();
			return obj;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public <T> T getBean(String id,Class<T> clazz){
		Object clazzBean = this.getBean(id);
		if(clazzBean.getClass().equals(clazz)){
			return (T)clazzBean;
		}
		return null;
	}
	
	public static void parse(){
		SAXBuilder builder = new SAXBuilder();
		Document document ;
		try {
			document = builder.build("src/main/resources/mybean.xml");
			Element root =  document.getRootElement();			
			List<Element> bean = root.getChildren();
			for(Element e:bean){
				if(!"bean".equals(e.getName())){
					continue;
				}
				String className = e.getAttributeValue("class");	
				String singleton = e.getAttributeValue("singleton");
				MyBeanDefinition md;
				
				md = new MyBeanDefinition();
				md.setClassName(className);
				md.setId(e.getAttributeValue("id"));
				if(singleton!=null){
					md.setSingleton(Boolean.parseBoolean(singleton));
				}
				list.add(md);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	
	public static void resolveBeanDefinition(){
		for(MyBeanDefinition m:list){
			String className = m.getClassName();
			String id = m.getId();
			try {
				Object bean = Class.forName(className).newInstance();
				if(m.isSingleton()){
					singletonBean.put(id, bean);
				}else{
					prototypeBean.put(id, m);
				}
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
